import { ajaxPromiseObject } from "../../../util/interface.js";
import dom, { ajax, domg } from "../../init.js";
import { obj } from "../../index.js";
import event from "./eventhandle.js";

export function initCreatedShuoshuo() {
  let str = `   
  <div class="shuoshuo">
    <h2>index: EditArticle</h2>
    <div id="test">
      <h1 id="articleTitle">title</h1>
      <p class='p1'>
        <span id="articleAuthor" class="iconfont icon-awardjiangzhang">${dom.user.name.innerText}</span>
        <span id="articleBrowesNum" class="iconfont icon-redu">1</span>
        <span id="articleComment" class="iconfont icon-pinglun">0</span>
        <span id="articleFontlength" class="iconfont icon-bianji">0</span>
        <span id="articleCreatedTime" class="iconfont icon-shijian_o">20201</span>
        <span>标签:</span>
        <span id="articleLabel">分享</span>
      </p>
      <hr>
      <div id="articleContent">无</div>
    </div>  
    <div class='t1'>
      <span>主标题 :</span>
      <input type="text" id="title" placeholder='标题不议过长' >
    </div>     
    <div class='t2'>
      <span>标签签 :</span>
      <input list="browsers" id="label"placeholder='可自定义' >
      <datalist id="browsers">
        <option value="分享">
        <option value="技术">
        <option value="日常">
      </datalist>
    </div>   
    <div id='editor'></div>
    <button class='editorbtn'>发布!</button>
    <input type="checkbox" value="预览" id='isyulan'><span>即时预览</span>
    <input type="checkbox" value="跳转" id='istoarticle'><span>发布后跳转到对应文章</span>
    <input type="checkbox" value="防盗" id='isimgtheft'><span>开启上传图片防盗链功能</span>
  </div>`;
  dom.content.content.innerHTML = str;
  const E = (window as any).wangEditor;
  const editor = new E("#editor");
  if (sessionStorage.getItem("isimgtheft")) {
    (domg("#isimgtheft") as any).checked = true;
    editor.config.uploadImgServer = "/api/user/theftarticleimg";
  } else {
    (domg("#isimgtheft") as any).checked = false;
    editor.config.uploadImgServer = "/api/user/articleimg";
  }
  editor.config.uploadFileName = "file";
  editor.config.uploadImgMaxLength = 1;
  editor.config.uploadImgMaxSize = 700 * 1024 * 1024;
  editor.config.uploadImgAccept = ["jpg", "jpeg", "png", "gif", "webp"];
  editor.config.uploadImgHooks = {
    customInsert: function (fun: any, result: any) {
      fun(result.data.url);
    },
  };
  editor.create();
  obj.editor = editor;
  //是否开启图片防盗链
  domg("#isimgtheft").onchange = event.isimgtheft;
  //绑定动态预览事件
  (document.querySelector("#isyulan") as HTMLElement).onclick = event.isyulan;
  if (sessionStorage.getItem("isyulan") !== null) {
    (document.querySelector("#isyulan") as any).checked = Boolean(
      sessionStorage.getItem("isyulan")
    );
    //手动刷新一次
    event.isyulan(1);
  }
  (document.querySelector(".editorbtn") as HTMLElement).onclick = event.send;
  //发布是否跳转到对应文章
  if (sessionStorage.getItem("istoarticle")) {
    (document.querySelector("#istoarticle") as any).checked = true;
  }
  (document.querySelector("#istoarticle") as HTMLElement).onclick =
    event.istoarticle;
}
